<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
    <meta http-equiv="Pragma" content="no-cache"/>
    <meta http-equiv="Expires" content="0"/>
    <title>收货信息 - 钢域云贸</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0">
    <link rel="icon" href="/uploads/logo.png" type="image/x-icon"/>
    <link rel="shortcut icon" href="/uploads/logo.png" type="image/x-icon"/>
    <link rel="stylesheet" type="text/css" href="/css/public.css"/>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        body {
            background-color: #f5f5f5;
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
        }
        .receipt-form, .receipt-list {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }
        .receipt-form h3, .receipt-list h3 {
            font-size: 18px;
            color: #333;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            font-size: 14px;
            color: #666;
            margin-bottom: 5px;
        }
        .form-group input, .form-group textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            box-sizing: border-box;
        }
        .form-group textarea {
            height: 80px;
            resize: none;
        }
        .form-group input[type="checkbox"] {
            width: auto;
            margin-right: 5px;
        }
        .btn-submit {
            background-color: #007bff;
            color: #fff;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .btn-submit:hover {
            background-color: #0056b3;
        }
        .address-item {
            border-bottom: 1px solid #eee;
            padding: 15px 0;
        }
        .address-item:last-child {
            border-bottom: none;
        }
        .address-header {
            display: flex;
            justify-content: space-between;
            font-size: 14px;
            color: #333;
            margin-bottom: 5px;
        }
        .default-tag {
            background-color: #28a745;
            color: #fff;
            padding: 2px 8px;
            border-radius: 10px;
            font-size: 12px;
        }
        .address-content {
            font-size: 14px;
            color: #666;
            margin-bottom: 5px;
        }
        .address-footer {
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 12px;
            color: #999;
        }
        .address-actions {
            display: flex;
            gap: 10px;
        }
        .btn-use {
            background-color: #007bff;
            color: #fff;
            border: none;
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
        }
        .btn-use:hover {
            background-color: #0056b3;
        }
        .btn-set-default {
            background-color: #6c757d;
            color: #fff;
            border: none;
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
        }
        .btn-set-default:hover {
            background-color: #5a6268;
        }
        .btn-edit {
            background-color: #ffc107;
            color: #fff;
            border: none;
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            text-decoration: none;
            display: inline-block;
        }
        .btn-edit:hover {
            background-color: #e0a800;
        }
        .btn-delete {
            background-color: #dc3545;
            color: #fff;
            border: none;
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
        }
        .btn-delete:hover {
            background-color: #c82333;
        }
    </style>
</head>
<body>
<th:block th:replace="~{/header.html}"></th:block>
<div class="container">
    <div class="receipt-form">
        <h3>新增收货地址</h3>
        <form id="receiptForm" th:action="@{/buyer/submitReceipt}" method="post">
            <div class="form-group">
                <label for="contactName">联系人</label>
                <input type="text" id="contactName" name="contactName" required>
            </div>
            <div class="form-group">
                <label for="contactPhone">联系电话</label>
                <input type="text" id="contactPhone" name="contactPhone" required>
            </div>
            <div class="form-group">
                <label for="deliveryAddress">收货地址</label>
                <textarea id="deliveryAddress" name="deliveryAddress" required></textarea>
            </div>
            <div class="form-group">
                <label>
                    <input type="checkbox" name="isDefault" value="1"> 设为默认地址
                </label>
            </div>
            <button type="submit" class="btn-submit">保存</button>
        </form>
    </div>
    <div class="receipt-list">
        <h3>我的收货地址</h3>
        <div class="address-item" th:each="receipt : ${receipts}">
            <div class="address-header">
                <span th:text="${receipt.contactName} + ' ' + ${receipt.contactPhone}"></span>
                <span class="default-tag" th:if="${receipt.isDefault} == 1">默认</span>
            </div>
            <div class="address-content" th:text="${receipt.deliveryAddress}"></div>
            <div class="address-footer">
                <span th:text="${#dates.format(receipt.createTime, 'yyyy-MM-dd HH:mm')}"></span>
                <div class="address-actions">
                    <form th:action="@{'/buyer/useReceipt/' + ${receipt.id}}" method="post" style="display: inline;">
                        <input type="hidden" name="userId" th:value="${userId}"/>
                        <button type="submit" class="btn-use">使用此地址</button>
                    </form>
                    <form th:action="@{'/buyer/setDefault/' + ${receipt.id}}"
                          method="post"
                          style="display: inline;">
                        <input type="hidden" name="userId" th:value="${userId}"/>
                        <button type="submit" class="btn-set-default">
                            <span th:text="${receipt.isDefault} == 1 ? '默认地址' : '设为默认'"></span>
                        </button>
                    </form>

                    <!-- 编辑按钮 -->
                    <a th:href="@{'/buyer/editReceipt/' + ${receipt.id}}" class="btn-edit">编辑</a>

                    <!-- 删除按钮 -->
                    <form th:action="@{'/buyer/deleteReceipt/' + ${receipt.id}}" method="post" style="display: inline;">
                        <input type="hidden" name="userId" th:value="${userId}"/>
                        <button type="submit" class="btn-delete"
                                onclick="return confirm('确定要删除这个收货地址吗？')">删除</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

</div>
<script th:inline="javascript">
    document.addEventListener('DOMContentLoaded', function() {
        // 打印所有表单信息
        const forms = document.querySelectorAll('form[method="post"]');
        console.log('找到的表单数量:', forms.length);
        forms.forEach((form, index) => {
            console.log(`表单${index}:`, form.action);
            // 绑定事件
            form.onsubmit = function(e) {
                console.log('表单提交触发:', this.action);
                return true;
            };
        });
    });
</script>
<th:block th:replace="~{/footer.html}"></th:block>
<script src="/js/jquery-latest.min.js"></script>
<script src="/js/popper.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
</body>
</html>